package net.aiform.account.action;

import javax.servlet.http.HttpServletRequest;

import net.aiform.account.pojo.Person;
import net.aiform.account.service.PersonService;
import net.aiform.bean.Constant;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.googlecode.jsonplugin.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.Preparable;

public class LoginAction implements Preparable, ServletRequestAware {
	private Logger log = Logger.getLogger(this.getClass());
	private PersonService service;
	private HttpServletRequest req;
	private Person person;
	private String certPicCode;
	private String arg1;

	public String execute() {
		log.info("before login:" + this.getPerson().toString());
		Person person = service.login(this.getPerson());
		log.info("after login:" + this.getPerson().toString());
		if (null == person) return Action.ERROR;
		this.req.getSession().setAttribute(Constant.LOGINUSER, person);
		return Action.SUCCESS;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public String getCertPicCode() {
		return certPicCode;
	}

	public void setCertPicCode(String certPicCode) {
		this.certPicCode = certPicCode;
	}

	public LoginAction(PersonService service) {
		this.service = service;
	}

	public void prepare() throws Exception {

	}

	@JSON(serialize = false)
	public String getArg1() {
		return arg1;
	}

	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.req = request;
	}

}
